/*
 * @Author: your name
 * @Date: 2021-12-22 19:39:48
 * @LastEditTime: 2021-12-22 21:19:05
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \briup_am\am-server\app\service\company.js
 */
const { Service } = require('egg')
const BussinessError = require('../utils/BussinessError')



class CompanyService extends Service {
    // 分页查询用户信息
    async pageQuery ({ page, pageSize }) {
        const { mysql } = this.app;
        
        //1. 查询分页数据
        let sql_select = `select * from match_company limit ${(page - 1) * pageSize},${pageSize}`
        const companys = await mysql.query(sql_select)
    
        //2. 统计数量
        const sql = `select count(*) as total from match_company`
        // 获取分页查询的数据条数
        const [{ total }] = await mysql.query(sql)
        // 返回
        return {
          page,
          pageSize,
          total,
          list: companys
        }
      }
      // 添加或修改用户信息
    async saveOrUpdate (row) {
        const { mysql } = this.app
        try {
          if (row.id) {
            // 更新 
            await mysql.update('match_company', row)
          } else {
            await mysql.insert('match_company', row)
          }
        } catch (error) {
          console.log(error);
          throw new BussinessError('操作失败')
        }
      }

      // 根据ID删除球队信息
  async deleteById (id) {
    const { mysql } = this.app
    try {
      await mysql.delete('match_team', { id })
    } catch (error) {
      throw error
    }
  }

// 接受球队参加比赛
async bindService ({ company_id, team_id, coach, captain, team_name }) {
  const { mysql } = this.app
  // 先根据球队ID查询其是否被接受，即判断设备的team_id是否为空，只能绑定状态为0(未绑定)状态的设备
  const team = await mysql.get('match_team', { id: team_id })
  if (team.company_id == null) {
    try {
      const row = {
        id: team_id,
        coach,
        captain,
        company_id,
        team_name
      }
      // 更新球队表 => engineer_id bind_status
      await mysql.update('master_team', row)
      // 更新工程表 => 工程状态
      await mysql.update('master_company', {
        id: company_id,
        status: '比赛中'
      })
    } catch (error) {
      throw new BusinessError('接受失败', 501)
    }
  }
}

// 拒绝球队加入比赛
async unbindDevice ({ company_id, team_id }) {
  const { mysql } = this.app

  try {
    // 获取球队的数据模拟器timer
    const { timer } = await mysql.get('match_team', { id: team_id })
    if (timer) {
      clearInterval(timer)
    }

    // 修改球队状态信息
    await mysql.update('match_team', {
      id: team_id,
      company_id: null,
      receive_state: 0,
      team_state: 0,
      team_name: null,
      coach: null,
      captain: null,
    })
  } catch (error) {
    throw new BusinessError('拒绝失败', 501)
  }
}

      // 查询所有球队
  async findEngineerDeviceTree ({ team_id, customer_id }) {
    const { mysql } = this.app
    let sql1 = `select id, team_name from match_team where 1=1`
    if (charge_id) {
      sql1 += ` and team_id = ${team_id}`
    }
    if (customer_id) {
      sql1 += ` and customer_id = ${customer_id}`
    }
    const sql2 = `select id, team_name, coach, captain from match_player`
    let engArr = await mysql.query(sql1)
    let devArr = await mysql.query(sql2)
    engArr.forEach(item => {
      item.children = []
      item.position = [item. coach, item.captain]
      delete item['coach']
      delete item['captain']
      devArr.forEach(i => {
        if (i.age == item.id) {
          let obj = {
            id: i.id,
            name: i.team_name,
            position: [i.coach, i.captain],
          }
          item.children.push(obj)
        }
      })
    })
    return engArr
  }

    }
    module.exports = CompanyService
    